package cn.zebra.dev.mall.module.collect.controller;

import cn.zebra.dev.common.constant.ParamsConstant;
import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.mall.module.collect.dto.ProductCollectDTO;
import cn.zebra.dev.mall.module.collect.vo.CollectVO;
import cn.zebra.dev.member.module.login.service.SecurityMemberService;
import cn.zebra.dev.product.module.collect.entity.MemberProductCollectEntity;
import cn.zebra.dev.product.module.collect.service.MemberProductCollectService;
import cn.zebra.dev.product.module.product.entity.ProductEntity;
import cn.zebra.dev.product.module.product.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author runnable@sina.cn
 */
@Api(tags = "商品收藏服务")
@RestController
@RequestMapping("/collect/product/")
public class ProductCollectController {

    @Autowired
    private SecurityMemberService memberService;

    @Autowired
    private MemberProductCollectService collectService;

    @Autowired
    private ProductService productService;

    @GetMapping("/list")
    @ApiOperation("获取收藏列表")
    public Result<PageUtils<ProductCollectDTO>> list(
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page) {
        Map<String, Object> map = new HashMap<>();
        map.put(ParamsConstant.LIMIT, limit);
        map.put(ParamsConstant.PAGE, page);
        PageUtils<MemberProductCollectEntity> pageUtils = collectService.findPage(map);
        List<MemberProductCollectEntity> list = pageUtils.getList();
        PageUtils<ProductCollectDTO> data = new PageUtils<>(pageUtils);
        if (list.size() > 0) {
            ArrayList<ProductCollectDTO> dtos = new ArrayList<>();
            List<Long> productIds = list.stream().map(MemberProductCollectEntity::getProductId).collect(Collectors.toList());
            List<ProductEntity> products = productService.findByIds(productIds);
            list.forEach(entity -> {
                Long productId = entity.getProductId();
                Optional<ProductEntity> optional = products.stream().filter(productEntity -> productEntity.getId().equals(productId)).findAny();
                if (optional.isPresent()) {
                    ProductEntity productEntity = optional.get();
                    ProductCollectDTO dto = new ProductCollectDTO();
                    dto.setProductId(productId);
                    dto.setProductImage(productEntity.getProductImage());
                    dto.setPrice(productEntity.getUnitPrice());
                    dto.setProductName(productEntity.getProductName());
                    dtos.add(dto);
                }
            });
            data.setList(dtos);
        }
        return Result.success(data);
    }

    @GetMapping("/isCollect/{productId}")
    @ApiOperation(value = "根据商品id获取该商品是否在收藏夹中")
    public Result isCollect(@PathVariable Long productId) {
        if (productService.findById(productId) == null) {
            return Result.error("该商品不存在!");
        }
        return Result.success(collectService.findByMemberIdAndProductId(memberService.getMemberId(), productId) != null);
    }

    @PostMapping("/save")
    @ApiOperation("加入商品收藏")
    public Result save(@Valid @RequestBody CollectVO vo) {
        if (collectService.saveCollect(memberService.getMemberId(), vo.getId())) {
            return Result.success("收藏成功!");
        }
        return Result.error("保存失败!");
    }

    @GetMapping("/delete/{id}")
    @ApiOperation("移除商品收藏")
    public Result delete(@PathVariable Long id) {
        if (collectService.deleteByIdAndMemberId(id, memberService.getMemberId())) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败!");
    }
}
